package com.totoro.springboot.druid.service.impl;

import com.totoro.springboot.druid.model.User;
import com.totoro.springboot.druid.repository.UserRepository;
import com.totoro.springboot.druid.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Niki on 2018/2/6 20:05
 */
@Service
@Transactional
public class UserServiceImpl implements IUserService{
    @Resource
    UserRepository userRepository;
    @Override
    public Long addUser(User user) {
        user = userRepository.save(user);
        return user.getId();
    }

    @Override
    public int deleteUser(Long id) {
        userRepository.delete(id);
        return 1;
    }

    @Override
    public void updateUser(User user) {
        userRepository.updateUser(user.getId(), user.getName(), user.getAge());
    }

    @Override
    public User findById(Long id) {
        return userRepository.findOne(id);
    }

    @Override
    public User findByName(String name) {
        return userRepository.findByName(name);
    }

    @Override
    public User findByNameAndAge(String name, Integer age) {
        return userRepository.findByNameAndAge(name, age);
    }

    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }
}
